<?php


namespace app\tt\controller;


use app\user\controller\Index;
use app\user\model\TtUser;
use think\Controller;
use think\Db;
use think\facade\Config;
use think\facade\Request;
use app\common\model\Movie as MovieModel;
use think\facade\Validate;

class Movie extends Controller
{

    protected $middleware = [
        'app\http\middleware\checkTTUserToken::class'
    ];


    //创建
    public function create()
    {

        $tt_user_id=Request::param('user_id');

        $user_info=TtUser::get($tt_user_id);

        if(!$user_info){
            $this->result($user_info,400,'失败');
        }

        $data=Request::only(['name','desc','cover','play_address']);
        $data['user_id']=$user_info->user_id;
        $data['tt_user_id']=$user_info->id ;
        $data['add_time']=time() ;

        $validate = Validate::make([
            'name|剧名'  => 'require|max:30',
            'desc|描述' => 'require',
            'cover|封面' => 'require|min:6',
        ]);

        if (!$validate->check($data)) {
            dump($validate->getError());
            $this->result('',400,$validate->getError());
        }
        $res=  MovieModel::create($data);

        if($res){
            $this->result($res,200,'成功');
        }else{
            $this->result('',400,'失败');
        }
    }

    //指定用户的电影列表
    public function index()
    {
        $tt_user_id= Request::param('user_id');
        $page=Request::param('page',1);
        $list= MovieModel::where('tt_user_id',$tt_user_id)->page($page,20)->order('id','desc')->select();
        $this->result($list,200,'成功');
    }

    //指定用户的电影列表
    public function read()
    {
        $movie_id= Request::param('movie_id');
        $movie= MovieModel::where('id',$movie_id)->find();

        if($movie){
          $res=  MovieModel::where('id',$movie_id)->setInc('views');
          $this->result($movie,200,'成功');
        }else{
            $this->result($movie,400,'没有这个电影呀');
        }
    }

    //指定用户的电影列表
    public function delete()
    {
        $movie_id= Request::param('movie_id');
        $movie= MovieModel::where('id',$movie_id)->delete();
        $this->result($movie,200,'成功');
    }

    //指定用户的电影列表
    public function rank()
    {
        $movie= MovieModel::where(['status'=>1])->field(['id','name','desc','cover','views','play_address'])->page(1,20)->order('views','desc')->select();
        $this->result($movie,200,'成功');
    }


}
